﻿/*

License:

    PDF UPLOAD METADATA EXTRACTOR for SharePoint 2013
    Copyright (C) 2012  Ingo Karstein

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

See my blog:
    http://blog.karstein-consulting.com
    
This project uses the iTextSharp v4.1.6 library that is licensed under LGPL and is published here:
    
    http://nuget.org/packages/iTextSharp-LGPL
    
    In Visual Studio 2012 just install iTextSharp v4.1.6 with NuGet. Open the "Package Manager Console"
    and type in "Install-Package iTextSharp-LGPL"
    
    http://itextsharp.svn.sourceforge.net/viewvc/itextsharp/tags/iTextSharp_4_1_6/

*/

using System;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Workflow;
using System.Collections.Generic;
using System.Diagnostics;

namespace ik.SharePoint2013.PDFUploadMetadataExtractor
{
    /// <summary>
    /// List Events
    /// </summary>
    public class ListEventReceiver : SPListEventReceiver
    {

        public static void AddListItemEventReceiver(SPList list, SPEventReceiverType erType)
        {
            Debugger.Launch();

            SPEventReceiverDefinition r;

            r = list.EventReceivers.Add();
            r.Assembly = typeof(ListItemEventReceiver).Assembly.FullName;
            r.Class = typeof(ListItemEventReceiver).FullName;
            r.Type = erType;

            r.SequenceNumber = 1000;
            r.Synchronization = SPEventReceiverSynchronization.Synchronous;
            r.HostType = SPEventHostType.List;
            r.HostId = list.ID;

            r.Update();

            list.Update();
        }

        public static void RemoveList(SPList list)
        {
            string c = typeof(ListItemEventReceiver).FullName.ToLower();

            List<SPEventReceiverDefinition> rs = new List<SPEventReceiverDefinition>();
            foreach( SPEventReceiverDefinition r in list.EventReceivers )
            {
                if( r.Class.ToLower() == c )
                    rs.Add(r);
            }

            bool somethingDone = false;

            foreach( var s in rs )
            {
                s.Delete();
                somethingDone = true;
            }

            if( somethingDone )
                list.Update();
        }

        /// <summary>
        /// A list was added.
        /// </summary>
        public override void ListAdded(SPListEventProperties properties)
        {
            base.ListAdded(properties);

            if( properties.List.BaseType == SPBaseType.DocumentLibrary )
            {
                AddListItemEventReceiver(properties.List, SPEventReceiverType.ItemAdded);
            }
        }


    }
}
